/**
 *
 */
package yl.hs.bmipfull.service.system.impl;

import org.springframework.beans.factory.annotation.Autowired;
import yl.hs.bmipfull.annotations.DBTable;
import yl.hs.bmipfull.daos.production.IProductionDispatchLogDao;
import yl.hs.bmipfull.daos.system.ISystemLogDao;
import yl.hs.bmipfull.pojo.production.ProductionDispatchLogEntity;
import yl.hs.bmipfull.pojo.system.SystemLogEntity;
import yl.hs.bmipfull.service.IBaseService;
import yl.hs.bmipfull.service.impl.BaseService;
import yl.hs.bmipfull.utils.HsApp;
import yl.hs.bmipfull.utils.datastate.ActionType;

import java.util.Date;

/**
 * @category
 *
 * @author 谢景广
 *
 */
public class SystemBaseService extends BaseService implements IBaseService {


    @Autowired
    private ISystemLogDao logDao;
    @Autowired
    private IProductionDispatchLogDao productlogdao;

    @Override
    public Integer writeLog(ActionType action, String dataSN, String content, String dataJson, Class<?> classType) {
        var mesg = content;
        var user = HsApp.getCurrent();
        var log = new SystemLogEntity();
        var userSN = HsApp.getClient();
        String userName = userSN;
        if (user != null) {
            userSN = user.getUserSN();
            userName = user.getUserName();
        }
        String objectName = "信息";
        DBTable dbTable = null;
        if (classType != null) {
            dbTable = classType.getAnnotation(DBTable.class);
        }


        if (dbTable != null) {
            objectName = dbTable.disName();
            log.setTypeCode(dbTable.mapCode());
            log.setTypeName(dbTable.disName());
        } else {
            log.setTypeName("系统写入");
            log.setTypeCode("SystemAutoWrite");
        }
        log.setUserSN(userSN);
        switch (action) {
            case PRINT -> {
                content = String.format("%s打印了编号为%s的%s信息", userName, dataSN, objectName);
                break;
            }
            case CREATE -> {
                content = String.format("%s创建了编号为%s的%s信息", userName, dataSN, objectName);
                break;
            }
            case UPDATE -> {
                content = String.format("%s更新了编号为%s的%s信息:%s", userName, dataSN, objectName, content);
                break;
            }
            case DELETE -> {
                content = String.format("%s删除了编号为%s的%s信息", userName, dataSN, objectName);
                break;
            }
            case EXPORT -> {
                content = String.format("%s导出了编号为%s的%s信息", userName, dataSN, objectName);
                break;
            }
            case IMPORT -> {
                content = String.format("%s导入了编号为%s的%s信息", userName, dataSN, objectName);
                break;
            }
            case QUERY -> {
                content = String.format("%s查询了编号为%s的%s信息", userName, dataSN, objectName);
                break;
            }
            default -> {
                content = String.format("%s操作了编号为%s的%s信息", userName, dataSN, objectName);
                break;
            }
        }
        log.setContent(content);
        log.setUserName(userName);
        log.setDataSN(dataSN);
        log.setActionType(action.code);
        log.setHandleTime(new Date());
        if (mesg.contains("库存")) {
            ProductionDispatchLogEntity entity = new ProductionDispatchLogEntity();
            entity.setUserName(userName);
            entity.setContent(mesg);
            entity.setActionType(action.code);
            entity.setDataSN(dataSN);
            entity.setHandleTime(new Date());
            entity.setUserSN(userSN);
            entity.setTypeCode(log.getTypeCode());
            entity.setTypeName(log.getTypeName());
            return productlogdao.insertNew(entity);
        } else {
            return logDao.insertNew(log);
        }
    }

    @Override
    public Integer writeLogs(ActionType action, String dataSN, String content, String dataJson, Class<?> classType) {
        return null;
    }

    @Override
    public void forWriteLog(String[] snList, short state) {
        var user = HsApp.getCurrent();
        String content = "";
        if (state == -2) {
            for (var str : snList) {
                writeLog(ActionType.UPDATE, str, user.getUserName() + "彻底删除了数据编号为" + str + "的数据信息", "", null);
            }
        } else {
            for (var str : snList) {
                writeLog(ActionType.UPDATE, str, user.getUserName() + "恢复删除了数据编号为" + str + "的数据信息", "", null);
            }
        }
    }

    public Integer writeLog(ActionType action, String[] snArray, Class<?> classType) {
        String objectName = "信息";
        var user = HsApp.getCurrent();

        DBTable dbTable = null;
        if (classType != null) {
            dbTable = classType.getAnnotation(DBTable.class);
        }
        var log = new SystemLogEntity();
        if (dbTable != null) {
            objectName = dbTable.disName();
            log.setTypeCode(dbTable.mapCode());
            log.setTypeName(dbTable.disName());
        } else {
            log.setTypeName("系统写入");
            log.setTypeCode("SystemAutoWrite");
        }
        var userSN = HsApp.getClient();
        String userName = userSN;
        if (user != null) {
            userSN = user.getUserSN();
            userName = user.getUserName();

        }
        log.setUserSN(userSN);
        var content = String.format("%s删除了编号为(%s)的%s信息", userName, String.join(",", snArray), objectName);
        log.setUserName(userName);
        log.setContent(content);
        log.setDataSN(String.join(",", snArray));
        log.setActionType(action.code);
        log.setHandleTime(new Date());
        return logDao.insertNew(log);
    }


}
